package com.liuya.common.exception;

import com.liuya.common.support.HttpCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;

/**
 * 自定义基础异常
 *
 * @author liuya
 * @date 20160824
 */
public abstract class BaseException extends RuntimeException {
    public BaseException() {
    }

    public BaseException(Throwable ex) {
        super(ex);
    }

    public BaseException(String message) {
        super(message);
    }

    public BaseException(String message, Throwable ex) {
        super(message, ex);
    }

    public void handler(ModelMap modelMap) {
        modelMap.put("httpCode", getHttpCode().value());
        if (StringUtils.isNotBlank(getMessage())) {
            modelMap.put("msg", getMessage());
        } else {
            modelMap.put("msg", getHttpCode().msg());
        }
        modelMap.put("timestamp", System.currentTimeMillis());
    }

    /**
     * 由子类重写
     * @return
     */
    protected HttpCode getHttpCode(){
        return null;
    }
}
